window.onload = function() {
    var form = document.querySelector('form')
    var username = document.querySelector('[name="username"]')
    var password = document.querySelector('[name="password"]')
    form.onsubmit = e => {
        if(!username.value) {
            popup.msg('用户名不能为空！')
            return false
        }
        if(!password.value) {
            popup.msg('密码不能为空！')
            return false
        }
        request({
            method: 'post',
            url: 'http://localhost:3000/front/api/users/login',
            data: {
                username: username.value,
                password: password.value
            }
        }).then(res => {
            // console.log(res);
            let {error_code, msg} = res
            
            if(error_code === 0) {

                let {username, userid, token} = res.data
                // setCookie('username', username, 7200)
                // setCookie('userid', userid, 7200)
                localStorage.setItem('token', token)
                localStorage.setItem('userid', userid, 7200)
                localStorage.setItem('username', username, 7200)
                let url = localStorage.getItem('url')
                popup.msg(msg, () => {
                    if(url) {
                        localStorage.removeItem('url')
                        location.href = url
                    } else {
                        location.href = './index.html'
                    }
                })
            } else{
                popup.msg(msg)
            }
        })
        return false
    }
}